package com.mosai.alignpro.util;

import android.content.Context;

import com.mosai.alignpro.R;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by Rays on 2020/3/26.
 */
public class TimeUtils {
    private final static long JUST = TimeUnit.MINUTES.toMillis(2);// 2分钟

    /**
     * 返回文字描述的日期
     */
    public static String getTimeText(Context context, Date date) {
        if (date == null) {
            return null;
        }
        Calendar current = Calendar.getInstance();
        Calendar target = Calendar.getInstance();
        target.setTime(date);
        // 不是同年
        if (current.get(Calendar.YEAR) != target.get(Calendar.YEAR)) {
            return DateUtils.format("yyyy-MM-dd HH:mm", date);
        }
        // 不是同月
        if (current.get(Calendar.MONTH) != target.get(Calendar.MONTH)) {
            return DateUtils.format("MM-dd HH:mm", date);
        }
        // 不是同周
        if (current.get(Calendar.WEEK_OF_YEAR) != target.get(Calendar.WEEK_OF_YEAR)) {
            return DateUtils.format("MM-dd HH:mm", date);
        }
        // 不是同日
        int currentDay = current.get(Calendar.DAY_OF_MONTH);
        int targetDay = target.get(Calendar.DAY_OF_MONTH);
        if (currentDay != targetDay) {
            // 昨日
            if (currentDay - 1 == targetDay) {
                return context.getString(R.string.time_yesterday, DateUtils.format("HH:mm", date));
            }
            int week = target.get(Calendar.DAY_OF_WEEK);
            switch (week) {
                case Calendar.MONDAY:
                    return context.getString(R.string.time_monday, DateUtils.format("HH:mm", date));
                case Calendar.TUESDAY:
                    return context.getString(R.string.time_tuesday, DateUtils.format("HH:mm", date));
                case Calendar.WEDNESDAY:
                    return context.getString(R.string.time_wednesday, DateUtils.format("HH:mm", date));
                case Calendar.THURSDAY:
                    return context.getString(R.string.time_thursday, DateUtils.format("HH:mm", date));
                case Calendar.FRIDAY:
                    return context.getString(R.string.time_friday, DateUtils.format("HH:mm", date));
                case Calendar.SATURDAY:
                    return context.getString(R.string.time_saturday, DateUtils.format("HH:mm", date));
                default:
                    return context.getString(R.string.time_sunday, DateUtils.format("HH:mm", date));
            }
        }
        // 2分钟内
        if (target.getTimeInMillis() - current.getTimeInMillis() < JUST) {
            return context.getString(R.string.time_just);
        }
        return DateUtils.format("HH:mm", date);
    }

}
